package pl.elka.pobr.ppr.logic;

import java.awt.Color;

public class Pixel {

    private static final int COLOR_TYPE_RED = 16;
    private static final int COLOR_TYPE_GREEN = 8;
    private static final int COLOR_TYPE_BLUE = 0;
    public int r;
    public int g;
    public int b;

    public Pixel(PixelHSV hsvPx) {
        this(Color.HSBtoRGB(hsvPx.h, hsvPx.s, hsvPx.v));
    }

    public Pixel(int pixelAsInt) {
        r = getColor(pixelAsInt, COLOR_TYPE_RED);
        g = getColor(pixelAsInt, COLOR_TYPE_GREEN);
        b = getColor(pixelAsInt, COLOR_TYPE_BLUE);
    }

    public Pixel(int rc, int gc, int bc) {
        r = rc;
        g = gc;
        b = bc;
    }

    private int getColor(int pixel, int colorType) {
        return ((pixel >> colorType) & 0xFF);
    }

    public int toInt() {
        int x = (0xFF << 24 | (int) r << COLOR_TYPE_RED | (int) g << COLOR_TYPE_GREEN | (int) b << COLOR_TYPE_BLUE);
        return x;
    }

    public static Pixel[][] create(int[] src, int width, int height) {
        Pixel[][] pixels = new Pixel[width][height];
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                pixels[j][i] = new Pixel(src[i * width + j]);
            }
        }
        return pixels;
    }
}
